import urllib.request
from lxml import etree


def send_request(url1):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181"
                      " Safari/537.36",
        "Cookie": "vjuids=2f2cbd5d.163be81ff19.0.94845df4dd737; vjlast=1527909581.1527909581.30; userNewsPort0=1; "
                  "Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1527909819; f_city=%E9%83%91%E5%B7%9E%7C101180101%7C; "
                  "Wa_lvt_1=1527909819"
    }
    request = urllib.request.Request(url1, headers=headers)
    response = urllib.request.urlopen(request).read()
    response = response.decode("utf-8")
    html1 = etree.HTML(response)
    return html1


def get_city_data(html1):
    date = html1.xpath('//div[@class="mainweather"]//h3/b/text()')
    weather = html1.xpath('//div[@class="mainweather"]//ul/li[@class="temp"]/text()[1]')
    high = html1.xpath('//div[@class="mainweather"]//ul/li[@class="temp"]/b/text()')
    air = html1.xpath('//div[@class="box_day"]//ul/li[@class="air"]//text()')
    info = html1.xpath('//div[@class="more_day"]/a[3]/text()')
    city_weather = ''
    for i in range(0, len(date)):
        city_weather = city_weather + str(date[i]) + ':' + str(weather[i]) + str(high[i]) +\
                       r'℃  ' + r'空气质量:' + str(air[i]) + '\n'
    weather_info = str(info[0]) + '\n' + city_weather + '\n'
    with open('天气预报.txt', 'a', encoding='utf-8') as v:
        v.write(weather_info)
        print(info[0] + "读取成功!")


if __name__ == '__main__':
    city_url = 'https://www.tianqi.com/nanjing'
    city_html = send_request(city_url)
    city_url = city_html.xpath('//ul[@class="raweather760"]/li/a/@href')
    city_url = city_url[1:26:2]
    print("The program is running....")
    for url in city_url:
        district_url = 'https://www.tianqi.com' + url
        html = send_request(district_url)
        get_city_data(html)
